import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne } from 'typeorm';
import { ProductSku } from './product-sku.entity';
import { Order } from './order.entity';

export enum InventoryOperationType {
  PURCHASE = 'purchase',
  SALE = 'sale',
  RETURN = 'return',
  ADJUSTMENT = 'adjustment',
  LOSS = 'loss',
}

@Entity('inventory_log')
export class InventoryLog {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column({ type: 'enum', enum: InventoryOperationType })
  operationType!: InventoryOperationType;

  @Column()
  quantity!: number;

  @Column({ type: 'decimal', precision: 10, scale: 2, nullable: true })
  unitPrice: number = 0;

  @Column({ type: 'text', nullable: true })
  remark: string = '';

  @Column({ type: 'json', nullable: true })
  metadata: Record<string, any> = {};

  @CreateDateColumn()
  createdAt!: Date;

  @UpdateDateColumn()
  updatedAt!: Date;

  @ManyToOne(() => ProductSku, sku => sku.inventoryLogs)
  sku!: ProductSku;

  @ManyToOne(() => Order, { nullable: true })
  order: Order | null = null;
}
